from libs.base.decorators import check_login, check_form
from libs.base.web import BaseHandler
from . import forms
from . import models


class LoginHandler(BaseHandler):

    @check_form(forms.LoginForm)
    async def post(self):
        """

        @api {POST} /api/v1.0/auth/login 登录
        @apiVersion 1.0.0
        @apiName Login
        @apiGroup AuthAccount
        @apiDescription 登录操作，获取后续通信用的auth_token，
            auth_token用作权限校验，放在header中 <br>
            e.g. header['Authorization'] = "Bearer " + auth_token <br>
            http请求包含参数时(即非get的其他类型请求)，
            请设置 header['Content-type'] = "application/json;charset=utf-8" <br>
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) account  账号
        @apiParam (Request-params) password  密码

        @apiParamExample {json} Request-Example:
        {
            "account": "xxxx",
            "password": "xxxx"
        }

        @apiSuccessExample {json} Success-Response:
        {
            "status":0,
            "message":"请求成功",
            "data":{
                "auth_token": "xxx.xxx.xxx"
            }
        }
        """
        model = models.LoginModel(self)
        res = await model.start(self.form_data)
        self.finish_auto(**res)


class UserInfoHandler(BaseHandler):

    # @check_login()
    async def get(self):
        """
        @api {GET} /api/v1.0/auth/user_info 获取用户权限
        @apiVersion 1.0.0
        @apiName GetUserPermsInfo
        @apiGroup AuthAccount
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiSuccess (Response-data) user_id  用户ID
        @apiSuccess (Response-data) account  账号
        @apiSuccess (Response-data) name  用户姓名
        @apiSuccess (Response-data) email  邮箱
        @apiSuccess (Response-data) phone_number  手机号码
        @apiSuccess (Response-data) create_time  账户创建时间
        @apiSuccess (Response-data) permissions  权限列表

        @apiSuccessExample {json} Success-Response:
        {
            "status": 0,
            "message": "请求成功",
            "data": {
                "user_id": 1,
                "account": "xxx",
                "name": "",
                "email": "",
                "phone_number": "",
                "create_time": "2019-06-24 16:46:24",
                "permissions": [
                    "demo_a",
                    "demo_admin",
                    "demo_b"
                ]
            }
        }
        """
        model = models.UserModel(self)
        res = await model.get_user_info()
        # res = {'status': 0, 'message': 'success', 'data': {
        #     "test": "ddd"
        # }}
        self.finish_auto(**res)
